package com.it.common.utils

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

object MD5Utils {
    fun md5(input: String): String {
        try {
            // 创建MD5加密对象
            val md = MessageDigest.getInstance("MD5")
            // 执行加密操作
            val messageDigest = md.digest(input.toByteArray())
            // 将字节数组转换为16进制字符串
            val hexString = StringBuilder()
            for (b in messageDigest) {
                val hex = Integer.toHexString(0xff and b.toInt())
                if (hex.length == 1) {
                    hexString.append('0')
                }
                hexString.append(hex)
            }
            // 返回加密后的字符串
            return hexString.toString()
        } catch (e: NoSuchAlgorithmException) {
            throw RuntimeException(e)
        }
    }
}